/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.model.operations;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import listo.client.model.IllegalOperationException;
import listo.utils.MiscUtils;
import listo.utils.types.Copyable;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Provides functionality used by the add and update ops.
 */
class Updater {

    public static class FieldSetting {
        @XStreamAsAttribute public final String name;
        @XStreamAsAttribute public String value;

        public FieldSetting(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }

    public static void addField(List<FieldSetting> fields, String name, String value) {
        if (value == null) value = "";
        for (Updater.FieldSetting field : fields) {
            if (field.name.equals(name)) {
                field.value = value;
                return;
            }
        }
        fields.add(new Updater.FieldSetting(name, value));
    }

    public static void addField(List<FieldSetting> fields, String name, Object value) {
        addField(fields, name, value != null ? value.toString() : null);
    }

    @SuppressWarnings(value = "unchecked")
    protected static void update(Copyable object, List<FieldSetting> fields) {
        Class<? extends Copyable> clazz = object.getClass();
        Method[] methods = clazz.getMethods();

        if (fields == null) {
            throw new RuntimeException("Illegal Add- or UpdateOp: no fields specified");
        }

        for (FieldSetting field : fields) {
            try {
                String setterMethodName = "set" + StringUtils.capitalize(field.name);
                Method setterMethod = null;
                for (Method method : methods) {
                    if (method.getName().equals(setterMethodName)) {
                        setterMethod = method;
                        break;
                    }
                }
                if (setterMethod == null) {
                    throw new IllegalOperationException("No field with name %1$s found for class %2$s", field.name,
                            clazz);
                }
                Class fieldType = setterMethod.getParameterTypes()[0];
                Object fieldValue = MiscUtils.convertFromString(field.value, fieldType);
                setterMethod.invoke(object, fieldValue);
            }
            catch (Exception e) {
                throw new IllegalOperationException(String.format("Error applying field value:\n %1$s", e));
            }
        }
    }

}
